package com.agv.backend.controller;

import com.agv.backend.entity.AjaxResult;
import com.agv.backend.entity.pojo.AgvUser;
import com.agv.backend.service.AgvUserService;
import com.agv.backend.service.impl.AgvUserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/agv/user")
@Slf4j
public class AgvUserController {

    private final AgvUserService agvUserService;

    public AgvUserController(AgvUserService agvUserService) {
        this.agvUserService = agvUserService;
    }

    @GetMapping
    public AjaxResult getUserByName(@RequestParam("name") String name) {
        log.info("Retrieving user with name: {}", name);
        AgvUser agvUser = agvUserService.getUserByName(name);
        log.info("Retrieved user: {}", agvUser);
        return AjaxResult.success(agvUser);
    }

    @PostMapping
    public void addUser(@RequestBody Map<String, List<String>> payload){
        List<String> users = payload.get("users");
        log.info("Received user list: {}", users);
        if(users != null && !users.isEmpty()){
            agvUserService.getUsers(users);
        }
    }
}
